package com.jake.spring.boot.webflux.controller;

import com.jake.spring.boot.webflux.model.School;
import com.jake.spring.boot.webflux.service.SchoolService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

@RestController
@RequestMapping("schools")
@Slf4j
public class SchoolController {

    @Autowired
    private SchoolService schoolService;

    @GetMapping
    public Flux<School> list() {
        return Flux.fromIterable(schoolService.list());
    }

    @GetMapping("{id}")
    public Mono<School> getById(@PathVariable Integer id) {
        return Mono.just(schoolService.getById(id));
    }

    @PostMapping
    public Mono<Integer> insert(@RequestBody School school) {
        return Mono.just(schoolService.insert(school));
    }

    @PutMapping
    public Mono<Integer> updateRow(@RequestBody School school) {
        return Mono.just(schoolService.updateRow(school));
    }

    @PatchMapping
    public Mono<Integer> updateName(@RequestBody School school) {
        return Mono.just(schoolService.updateName(school));
    }
}
